import axios from "axios";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/stores/user.js";
import { TIME_OUT, BASE_URL } from "./config.js";
let userStore = useUserStore();
// import { useRouter } from "vue-router";
// const router = useRouter();


import router from "../router/index.js"; // 这边进行路由的导入
function throttleToLogin() {
    console.log(userStore);
    userStore.clearUserInfo();
    localStorage.clear();
    router.push({
        path: '/login'
    })
}

function requestError(response) {
    return new Promise((_, reject) => {
        const { data } = response;
        const msg = __DEV__ ? `api请求出错 ${response.config.url}` : data.msg;
        ElMessage({
            message: msg,
            type: "error",
        });
        reject(data);
    });
}

export default function request(config) {
    // 设置请求配置
    const instance = axios.create({
        // baseURL: import.meta.env.VITE_APP_API_BASEURL,
        baseURL: BASE_URL,
        timeout: TIME_OUT,
    });

    instance.interceptors.request.use((config) => {
        // 配置请求头
        config.headers["user-token"] = userStore.token;
        return config;
    });

    instance.interceptors.response.use(
        (response) => {
            const dataAxios = response.data;
            // const { code, data } = dataAxios;
            switch (`${dataAxios.code}`) {
                // code === 200 代表没有错误
                case "200":
                case "1":
                    return dataAxios;
                // code === 400001004 代表token 过期打回登录页
                case '0':
                case '-900':
                case '-999':
                case '-2':
                    ElMessage({
                        message: dataAxios.msg,
                        type: "error",
                    });
                    return dataAxios;
                    break;
                case "-201":
                    // 打回登录页等方法，要根据情况修改
                    throttleToLogin();
                    ElMessage({
                        message: dataAxios.msg,
                        type: "error",
                    });
                    break;
                case "10501":
                    ElMessage({
                        message: '服务器错误',
                        type: "error",
                    });
            }
        },
        (error) => {
            let errorMsg = "";
            if (error && error.response) {
                switch (error.response.status) {
                    case 400:
                        errorMsg = '请求错误';
                        break
                    case 408:
                        errorMsg = '请求超时';
                        break
                    case 500:
                        errorMsg = '服务器错误';
                        break
                    case 10501:
                        errorMsg = '服务器错误';
                        break
                    default:
                        errorMsg = '请求错误';
                }
            }
            ElMessage({
                message: errorMsg,
                type: "error",
            });
            return Promise.reject(error);
        }
    );
    return instance(config);
}